package com.py.pythink.third;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;

/**
 * @author:
 * @date： 2021-07-04 10:20
 */
public class AliSmsUtil {
    //平台的密钥 不要公开到公众平台
    private static final String accessKeyId="LTAI5t8QA19CRR2V15oHyHW8";//
    private static final String accessKeySecret="OqUKmRFUnAYm0frzUc43uKnk5BNoaP";//
    public static final String TEM_ADD_CODE="SMS_114390520";
    public static final String TEM_LOGIN_CODE="SMS_115250125";
    public static final String TEM_FIND_CODE="SMS_204975067";
    /**
     * 发送短信验证码,注册时使用
     * @param phone 手机号
     * @param code 短信验证码*/
    public static boolean sendRegisterCode(String phone,int code){
        //return sendSms(phone,TEM_ADD_CODE,code+"");
        System.err.println("短信验证码："+code);
        return true;
    }
    /**
     * 发送短信验证码,登陆时使用
     * @param phone 手机号
     * @param code 短信验证码*/
    public static boolean sendLoginCode(String phone,int code){
       return sendSms(phone,TEM_LOGIN_CODE,code+"");
//        System.err.println("短信验证码："+code);
//        return true;
    }
    /**
     * 发送短信验证码,找回密码时使用
     * @param phone 手机号
     * @param code 短信验证码*/
    public static boolean sendFindPassCode(String phone,int code){
        return sendSms(phone,TEM_FIND_CODE,code+"");
    }
    /**
     * 发送短信验证码
     * @param phone 手机号
     * @param msg 短信验证码*/
    private static boolean sendSms(String phone,String temcode,String msg){
        //1.创建配置对象
        Config config = new Config()
                .setAccessKeyId(accessKeyId)
                .setAccessKeySecret(accessKeySecret);
        //2.访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        Client client;
        try {
            //3.实例化客服端对象
            client = new Client(config);
            //4.创建请求对象，并设置信息
            SendSmsRequest sendSmsRequest = new SendSmsRequest()
                    .setPhoneNumbers(phone)
                    .setSignName("来自邢朋辉的短信")
                    .setTemplateCode(temcode)
                    .setTemplateParam("{\"code\":\""+msg+"\"}");
            //5.发送短信验证码
            SendSmsResponse response=client.sendSms(sendSmsRequest);
            System.err.println(response.getBody().message);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}